package com.djmw.mouse.web.hr;

import java.util.Map;

import javax.servlet.ServletRequest;
import javax.validation.Validator;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;

import com.djmw.mouse.Env;
import com.djmw.mouse.entity.Application;
import com.djmw.mouse.entity.HrUser;
import com.djmw.mouse.entity.Position;
import com.djmw.mouse.service.ApplicationService;
import com.djmw.mouse.service.CvService;
import com.djmw.mouse.service.PositionService;
import com.djmw.mouse.service.account.ShiroDbRealm.ShiroUser;
import com.google.common.collect.Maps;

/**
 * 查看投递简历的Controller, 使用Restful风格的Urls:
 * 
 * 列表: GET /hr/application <br>
 * 
 * @author michael
 */

@Controller
@RequestMapping(value = "/hr/application")
public class ApplicationCvController {

    private static Map<String, String> sortTypes = Maps.newLinkedHashMap();
    static {
        sortTypes.put("auto", "申请日期");
    }

    @Autowired
    private PositionService positionService;
    @Autowired
    private CvService cvService;
    @Autowired
    private ApplicationService applicationService;
    // @Autowired
    // private MemberAccountService memberAccountService;
    @Autowired
    private Validator validator;
    @Autowired
    private Env env;;

    @RequestMapping(value = "")
    public String list(@RequestParam(value = "positionId", defaultValue = "0") final Long positionId,
            @RequestParam(value = "sortType", defaultValue = "auto") String sortType,
            @RequestParam(value = "sortDir", defaultValue = "d") String sortDir,
            @RequestParam(value = "page", defaultValue = "1") int pageNumber,
            @RequestParam(value = "pageSize", defaultValue = "0") int pageSize, Model model, ServletRequest request,
            RedirectAttributes redirectAttributes) {
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        HrUser hrUser = getCurrentUser();
        // ACL
        if (positionId != 0) {
            Position position = positionService.getPosition(positionId);
            if (position == null) {
                redirectAttributes.addFlashAttribute("message", "无此职位");
                return "redirect:/hr/application/";
            }
            if (position.getCompany().getId() != hrUser.getCompany().getId()) {
                redirectAttributes.addFlashAttribute("message", "无权查看");
                return "redirect:/hr/application/";
            }
        }
        Page<Application> applications;
        if (pageSize == 0) {
            pageSize = env.getPageSize().getHrAppPageSize();
        }
        if (positionId == 0) {
            applications = applicationService.getApplicationByCompanyId(hrUser.getCompany().getId(), searchParams,
                    pageNumber, pageSize, sortType, sortDir);
        } else {
            applications = applicationService.getApplicationByPositionId(positionId, searchParams, pageNumber,
                    pageSize, sortType, sortDir);
        }
        Page<Position> positions = positionService.getCompanyPosition(hrUser.getCompany().getId());
        model.addAttribute("positionId", positionId);
        model.addAttribute("applications", applications);
        model.addAttribute("positions", positions);
        model.addAttribute("positionsWrapper", new Object() {
            public Long getPositionId() {
                return positionId;
            }
        });
        model.addAttribute("sortType", "auto");
        model.addAttribute("sortTypes", sortTypes);
        Servlets.encodeParameterStringWithPrefix(searchParams, "search_");
        return "hr/application/application-list";
    }

    public HrUser getCurrentUser() {
        ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
        HrUser hrUser = (HrUser) user.getUser();
        return hrUser;
    }
}
